﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class QuestionClassDal
	{
		IDBHelp db = DBHelp.Create();
		Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="QuestionClassID">标识列ID</param>
		public bool Exists(int QuestionClassID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [QuestionClass] where QuestionClassID=@questionclassid ");
			htSyn.Add("@questionclassid", QuestionClassID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<QuestionClass> GetModel()
		{			
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [QuestionClassID],[QuestionFid],[QuestionClassName] FROM [QuestionClass]");
			 return getQuestionClassBySql(strSql.ToString());
		}
		#endregion

        #region 根据父id得到数据列表---郑同飞
        /// <summary>
        /// #region 根据父id得到数据列表---郑同飞
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public IList<QuestionClass> GetModelByFid(int fid)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("Select [QuestionClassID],[QuestionFid],[QuestionClassName],[QuestionState] FROM [QuestionClass]");
            strSql.Append(" where [QuestionFid]=@questionid");
            htSyn.Add("@questionid",fid);
            return getQuestionClassBySql(strSql.ToString(), htSyn);
        }
        #endregion

        #region 根据标识列查询实体
        /// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="QuestionClassID">标识列ID</param>
		/// <returns></returns>
		public QuestionClass GetModel(int questionclassid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [QuestionClassID],[QuestionFid],[QuestionClassName],[QuestionState] FROM [QuestionClass]");
			strSql.Append(" where QuestionClassID=@questionclassid ");
			htSyn.Add("@QuestionClassID", questionclassid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			QuestionClass questionclassModel = new QuestionClass();
			if (dt.Rows.Count > 0)
			{
				questionclassModel.questionFid=Convert.ToInt32(dt.Rows[0]["QuestionFid"]);
				questionclassModel.questionClassName=dt.Rows[0]["QuestionClassName"].ToString();
                questionclassModel.Questionstate =Convert.ToBoolean(dt.Rows[0]["QuestionState"]);
			}
			return questionclassModel;
		}
		#endregion

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="QuestionClass">实体</param>
		/// <returns></returns>
		public bool Update(QuestionClass questionclassModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [QuestionClass] set ");
			strSql.Append("[QuestionFid]=@questionFid,");
			strSql.Append("[QuestionClassName]=@questionClassName,");
            strSql.Append("[QuestionState]=@questionstate");
            strSql.Append(" where QuestionClassID=@questionclassid ");			
			htSyn.Add("@questionfid", questionclassModel.questionFid);
			htSyn.Add("@questionclassname", questionclassModel.questionClassName);
            htSyn.Add("@questionstate",questionclassModel.Questionstate);
            htSyn.Add("@questionclassid",questionclassModel.questionClassID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

        #region 批量修改---郑同飞
        /// <summary>
        /// 批量修改---郑同飞
        /// </summary>
        /// <param name="id"></param>
        /// <param name="qclassid"></param>
        /// <returns></returns>
        public bool Update(string[] id,string qclassid)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            strSql.Append("update QuestionClass set");
            strSql.Append(" QuestionFid=@questionclassid");
            strSql.Append(" where ");
            foreach (string  str in id)
	        {
        	    strSql.AppendFormat(" QuestionClassID={0} or",str);	 
	        }
            strSql.Remove(strSql.Length - 1, 1);
            strSql.Remove(strSql.Length - 1, 1);
            htSyn.Add("@questionclassid",qclassid);
            if (db.ExecuteNonQuery(strSql.ToString(),htSyn)>0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除信息
        /// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="QuestionClassID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int QuestionClassID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [QuestionClass] where QuestionClassID=@questionclassid");
			htSyn.Add("questionclassid", QuestionClassID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="QuestionClass">实体</param>
		/// <returns></returns>
		public bool Insert(QuestionClass questionclassModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [QuestionClass]([QuestionFid],[QuestionClassName],[QuestionState]) values (@questionfid,@questionclassname,@questionstate)");
			htSyn.Add("@questionfid", questionclassModel.questionFid);
			htSyn.Add("@questionclassname", questionclassModel.questionClassName);
            htSyn.Add("@questionstate",questionclassModel.Questionstate);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<QuestionClass> getQuestionClassBySql(string strSql)
		{
			IList<QuestionClass> list = new List<QuestionClass>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					QuestionClass questionclassModel = new QuestionClass();
					questionclassModel.questionClassID= Convert.ToInt32(dr["QuestionClassID"]);
					questionclassModel.questionFid= Convert.ToInt32(dr["QuestionFid"]);
					questionclassModel.questionClassName=dr["QuestionClassName"].ToString();
                    questionclassModel.Questionstate =Convert.ToBoolean (dr["QuestionState"]);
					list.Add(questionclassModel);
				}
			}
			return list;
		}
		#endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        /// <param name="strSql">sql语句</param>
        public IList<QuestionClass> getQuestionClassBySql(string strSql,Hashtable ht)
        {
            IList<QuestionClass> list = new List<QuestionClass>();
            DataTable dt = db.GetDataTable(strSql,ht);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    QuestionClass questionclassModel = new QuestionClass();
                    questionclassModel.questionClassID = Convert.ToInt32(dr["QuestionClassID"]);
                    questionclassModel.questionFid = Convert.ToInt32(dr["QuestionFid"]);
                    questionclassModel.questionClassName = dr["QuestionClassName"].ToString();
                    questionclassModel.Questionstate =Convert.ToBoolean(dr["QuestionState"]);
                    list.Add(questionclassModel);
                }
            }
            return list;
        }
        #endregion
    }
}
